package org.xnsg.app.core.helper;

import com.eova.common.utils.xx;
import com.eova.model.Menu;
import com.eova.model.User;
import com.eova.service.sm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xnsg.app.core.HttpCode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author zhaopan
 * @date :2017/4/16
 * @Description:登录帮助类
 */
public class LoginHelper {
    private final static Logger logger = LoggerFactory.getLogger(LoginHelper.class);
    public static List<String> alowMenus=new ArrayList<String>();
    static{
        alowMenus.add("m_aj_jydw");
        alowMenus.add("m_aj_sj_mgr");
        alowMenus.add("m_aj_rcxc");
        alowMenus.add("m_aj_zxxc");
        alowMenus.add("m_aj_wgxx");
        alowMenus.add("m_aj");
    }
    /**
     *  获得user的菜单列表
     * @param map
     * @param user
     */
    public static void getMenuList(Map resultMap,User user){

        if(user != null) {
            int rid = user.getInt("rid").intValue();
            List cacheList = Menu.dao.queryRoot();
            ArrayList rootList = new ArrayList(cacheList);
            Iterator it = rootList.iterator();

            while(it.hasNext()) {
                Menu x = (Menu)it.next();
                boolean flag = sm.auth.isExistsAuthByPidRid(x.getInt("id").intValue(), rid);
                if(!flag) {
                    it.remove();
                }
            }

            if(xx.isEmpty(rootList)) {
                resultMap.put("msg", "当前角色没有任何功能授权！");
                resultMap.put("success", true);
                resultMap.put("httpCode", HttpCode.FORBIDDEN);
            } else {
                resultMap.put("data", rootList);//返回根菜单
                resultMap.put("httpCode", HttpCode.OK);
                resultMap.put("success", true);
            }
        } else {
            resultMap.put("msg", "无法获得根菜单！");
            resultMap.put("httpCode", HttpCode.LOGIN_FAIL);
            resultMap.put("success", false);
        }
    }
}
